<?php
// +----------------------------------------------------------------------
// | PublicAction.class.php - MyPT
// +----------------------------------------------------------------------
// | Copyleft (c) 2010 By Asxzy All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Asxzy <asxzy@163.com>
// +----------------------------------------------------------------------
class PublicAction extends CommonAction{
	function _initialize() {
		if (! $_SESSION [C ( 'USER_AUTH_KEY' )]) {
			// 检查前台是否登录
			$this->assign("jumpUrl",'__ROOT__');
		    $this->error('请先登录前台');
		}
	}
	// 首页跳转
	public function index(){
		if(!isset($_SESSION[C('ADMIN_AUTH_KEY')])) {
			$this->display();
		}else{
			$this->redirect('__APP__');
		}
	}
	
	// 登录页面	
	public function login(){	
		if(!isset($_SESSION[C('ADMIN_AUTH_KEY')])) {
			$this->display();
		}else{
			$this->assign("jumpUrl",'__APP__');
        	$this->error('你已经登录过了,请不要重复登陆');
		}
	}
	
	// 登录验证
	public function checklogin(){
		// 检查验证码
		if($_SESSION['verify'] != md5($_POST['verify'])) {
			$this->error('验证码错误！');
		}
		//生成认证条件
        $map = array();
		// 支持使用绑定帐号登录
		$map['uid'] = $_SESSION['userid'];
		$map['username'] = $_POST['username'];
		$map['password'] = md5($_POST['password']);
        $map["status"] = 'confirmed';
		import('ORG.Util.RBAC');
        $authInfo = RBAC::authenticate($map,'admin');
        //使用用户名、密码和状态的方式进行认证
        if(!isset($authInfo)) {
        	$this->error('用户名或密码错误');
        }else{
        	// 将用户信息写入SESSION
            $_SESSION['ADMIN_AUTH_KEY'] = $authInfo['id'];
            $_SESSION['$adminInfo'] = $authInfo['id'];
            $_SESSION['administrator'] = true;
            //保存登录信息
			$User = M('admin');
            $data = array();
			$data['id'] = $authInfo['id'];
			$data['login_count'] = array('exp','login_count+1');
			$data['ip'] = get_client_ip();
	    	if(!$User->save($data)){
	    		$this->error('登录日志写入错误');
	    	}
            $this->assign("jumpUrl",'__APP__');
			$this->success('登录成功！');
		}
	}
	
	// 退出登录
    public function logout()
    {
        if(isset($_SESSION[C('ADMIN_AUTH_KEY')])) {
			unset($_SESSION[C('ADMIN_AUTH_KEY')]);
            $this->assign("jumpUrl",'__APP__');
            $this->success('登出成功！');
        }else{
            $this->assign("jumpUrl",'__APP__/Public/login/');
        	$this->error('你还未登录！');
        }
    }

	// 生成验证码
	public function verify(){
		import('ORG.Util.Image');
		Image::buildImageVerify();
	}


}
?>